package com.tensquare.qa.controller;
import java.util.List;
import java.util.Map;

import com.tensquare.qa.client.LabelClient;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.tensquare.qa.pojo.Problem;
import com.tensquare.qa.service.ProblemService;

import entity.PageResult;
import entity.Result;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * 控制器层
 * @author Administrator
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/problem")
public class ProblemController {

	@Autowired
	private ProblemService problemService;
	@Autowired
	private JwtUtil jwtUtil;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private LabelClient labelClient;
	
	
	/**
	 * 查询全部数据
	 * @return
	 */
	@RequestMapping(method= RequestMethod.GET)
	public Result findAll(){
		return new Result(true,StatusCode.OK,"查询成功",problemService.findAll());
	}
	
	/**
	 * 根据ID查询
	 * @param id ID
	 * @return
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.GET)
	public Result findById(@PathVariable String id){
		return new Result(true,StatusCode.OK,"查询成功",problemService.findById(id));
	}

	/**
	 * 根据ID查询Label
	 * @param id ID
	 * @return
	 */
	@RequestMapping(value="/label/{id}",method= RequestMethod.GET)
	public Result findLabelById(@PathVariable String id){
		return labelClient.findById(id);
	}




	/**
	 * 分页查询全部数据
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/{page}/{size}",method=RequestMethod.GET)
	public Result findPage(@PathVariable int page,@PathVariable int size){
		Page<Problem> pageList = problemService.findPage(page, size);
		return new Result(true,StatusCode.OK,"查询成功",new PageResult<Problem>(pageList.getTotalElements(), pageList.getContent() ) );
	}


	/**
	 * 分页+多条件查询
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/{page}/{size}",method=RequestMethod.POST)
	public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
		Page<Problem> pageList = problemService.findSearch(searchMap, page, size);
		return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<Problem>(pageList.getTotalElements(), pageList.getContent()) );
	}

	/**
	 * 增加
	 * @param problem
	 */
	@RequestMapping(method=RequestMethod.POST)
	public Result add(@RequestBody Problem problem){
		//判断当前用户是否登录
		Claims claims_user = (Claims) request.getAttribute("claims_user");
		if (claims_user == null) {
			//未登录 => 提示先登录再操作
			throw new RuntimeException("请用户先登录");
		}
		//已登录 => 将用户ID放到problem对象中
		problem.setUserid(claims_user.getId());
		problemService.add(problem);
		return new Result(true, StatusCode.OK,"增加成功");
	}
	
	/**
	 * 修改
	 * @param problem
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.PUT)
	public Result update(@RequestBody Problem problem, @PathVariable String id ){
		problem.setId(id);
		problemService.update(problem);		
		return new Result(true,StatusCode.OK,"修改成功");
	}
	
	/**
	 * 删除
	 * @param id
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
	public Result delete(@PathVariable String id ){
		problemService.deleteById(id);
		return new Result(true,StatusCode.OK,"删除成功");
	}


	/**
	 * 最新问题列表
	 * @param label 问题所在标签id
	 * @param page  当前页数
	 * @param size  每页条数
	 * @return
	 */
	@RequestMapping(value = "/newlist/{label}/{page}/{size}",method = RequestMethod.GET)
	public Result newList(@PathVariable String label, @PathVariable Integer page, @PathVariable Integer size) {
		return new Result(true,StatusCode.OK,"查询成功",problemService.newList(label,page,size));
	}

	/**
	 * 热门问题列表
	 * @param label 问题所在标签id
	 * @param page  当前页数
	 * @param size  每页条数
	 * @return
	 */
	@RequestMapping(value = "/hotlist/{label}/{page}/{size}",method = RequestMethod.GET)
	public Result hotList(@PathVariable String label, @PathVariable Integer page, @PathVariable Integer size) {
		return new Result(true,StatusCode.OK,"查询成功",problemService.hotList(label,page,size));
	}

	/**
	 * 等待回答问题列表
	 * @param label 问题所在标签id
	 * @param page  当前页数
	 * @param size  每页条数
	 * @return
	 */
	@RequestMapping(value = "/waitlist/{label}/{page}/{size}",method = RequestMethod.GET)
	public Result waitList(@PathVariable String label, @PathVariable Integer page, @PathVariable Integer size) {
		return new Result(true,StatusCode.OK,"查询成功",problemService.waitList(label,page,size));
	}
}
